<script setup lang="ts">
import { useRoute } from 'vue-router'
import type { FormInstance } from 'element-plus'
import { searchHouseByTitle } from '~/api/house'

const route = useRoute()
const router = useRouter()
const ruleFormRef = ref<FormInstance>()

const houses = ref([])
const title = route.params.name as string
console.log(title)

watch(() =>
  router.currentRoute.value.path, (toPath) => {
  // 要执行的方法
  console.log(toPath)
  const query = router.currentRoute.value.params.name
  refreshData(query)
}, { immediate: true, deep: true })

async function refreshData(title: any) {
  await searchHouseByTitle(title).then((res: any) => {
    houses.value = res.result
  })
}
refreshData(title)

function clickCard(id: number) {
  router.push(`/house/${id}`)
}

const ruleForm = reactive({
  price: '',
  stars: '',
  comment: '',
})

async function onSubmit() {
  refreshData(title)
  if (ruleForm.price) {
    await searchHouseByTitle(title, ruleForm.price).then((res: any) => {
      houses.value = res.result
    })
  }
  else if (ruleForm.stars) {
    await searchHouseByTitle(title, null, ruleForm.stars).then((res: any) => {
      houses.value = res.result
    })
  }
  else if (ruleForm.price && ruleForm.stars) {
    await searchHouseByTitle(title, ruleForm.price, ruleForm.stars).then((res: any) => {
      houses.value = res.result
    })
  }
  else {
    refreshData(title)
  }
}

function onReset(formEl: FormInstance | undefined) {
  if (!formEl)
    return
  formEl.resetFields()
  refreshData(title)
}
</script>

<template>
  <div class="main">
    <!-- 筛选 -->
    <div>
      <el-form ref="ruleFormRef" :model="ruleForm" label-width="auto">
        <el-row :gutter="100">
          <el-col :span="7">
            <el-form-item label="价格">
              <el-radio-group v-model="ruleForm.price">
                <el-radio value="1">
                  200以上
                </el-radio>
                <el-radio value="2">
                  100到200
                </el-radio>
                <el-radio value="3">
                  100以内
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="评分">
              <el-radio-group v-model="ruleForm.stars">
                <el-radio value="1">
                  4.8以上
                </el-radio>
                <el-radio value="2">
                  4.5以上
                </el-radio>
                <el-radio value="3">
                  4.2以上
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="7">
            <el-form-item>
              <el-button type="primary" @click="onSubmit">
                筛选
              </el-button>
              <el-button @click="onReset(ruleFormRef)">
                重置
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <!-- 瀑布流 -->
    <div>
      <wc-waterfall class="mt-5" :gap="25" :cols="4">
        <div
          v-for="item in houses as any"
          :key="item.id"
          :body-style="{ padding: '0px' }"
          class="card"
          @click="clickCard(item.id)"
        >
          <img :src="item.img" class="pic">
          <div class="parent">
            <div class="div1">
              <p class="title view-text">
                {{ item.title }}
              </p>
            </div>
            <div class="div2 mt-2 text-sm">
              <p class="flex items-center font-bold">
                <img src="/star_.png" class="mr-1 h-4 w-4">{{ item.stars }}
              </p>
            </div>
            <div class="div3">
              <p class="des view-text">
                {{ item.des }}
              </p>
            </div>

            <div class="div4">
              <p class="flex items-center">
                <img src="/address.png" class="mr-1 h-4 w-4">
                {{ item.address }}
              </p>
            </div>
            <div class="div5 text-sm font-bold">
              <p>￥{{ item.price }} 晚</p>
            </div>
          </div>
        </div>
      </wc-waterfall>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.main {
  padding: 10px 45px;
}
.pic {
  border-radius: 0.375rem 0.375rem 0 0;
}
.parent {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  padding: 0 10px 10px;
}

.div1 {
  grid-area: 1 / 1 / 2 / 2;
}
.div2 {
  grid-area: 1 / 2 / 2 / 3;
  justify-self: end;
}
.div3 {
  grid-area: 2 / 1 / 3 / 3;
  place-content: center center;
}
.div4 {
  grid-area: 3 / 1 / 4 / 2;
}
.div5 {
  grid-area: 3 / 2 / 4 / 3;
  justify-self: end;
}
.view-text {
  display: inline-block;
  white-space: nowrap;
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}

.treasure {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  color: #fff;
  padding: 20px 40px;
  height: 100%;
  overflow: auto;
  background-color: #ffffff;
  @media screen and (max-width: 510px) {
    padding: 20px;
  }
}
.card {
  transition: all 0.5s;
  border-radius: 0.375rem;
}
.card:hover {
  box-shadow: 1px 1px 15px 3px rgba(0, 0, 0, 0.4);
}
.title {
  /* 超出10个字隐藏，之所以设置11em是因为省略号占一个位置 */
  width: 12em;
  overflow: hidden;
  /* 显示省略符号来代表被修剪的文本。 */
  text-overflow: ellipsis;
  /* 文本不换行 */
  white-space: nowrap;
  font-size: 1em;
  color: #000000;
  padding: 5px 0 5px 0;
}
.des {
  font-size: 13px;
  color: #717171;
}

.scrollbar-flex-content {
  display: flex;
  color: #000000;
  font-size: 12px;
}
.scrollbar-demo-item {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100px;
  height: 50px;
  margin: 10px;
  text-align: center;
  transition: all 0.8s;
}
.scrollbar-demo-item:hover {
  box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.4);
}
</style>
